from PyQt5.QtWidgets import (
    QWidget, QTabWidget, QVBoxLayout, 
    QLabel, QTextBrowser, QScrollArea
)

class PreviewPanel(QWidget):
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()
        self.tabs = QTabWidget()
        
        # 元数据面板
        self.meta_widget = self._create_meta_widget()
        # 内容预览面板
        self.content_preview = QTextBrowser()
        
        self.tabs.addTab(self.meta_widget, "文档元数据")
        self.tabs.addTab(self.content_preview, "内容预览")
        
        layout.addWidget(self.tabs)
        self.setLayout(layout)

    def _create_meta_widget(self):
        widget = QScrollArea()
        content = QWidget()
        layout = QVBoxLayout()
        
        self.meta_labels = {
            'title': QLabel("标题："),
            'pages': QLabel("页数："),
            'author': QLabel("作者："),
            'platform': QLabel("来源：")
        }
        
        for label in self.meta_labels.values():
            layout.addWidget(label)
        
        content.setLayout(layout)
        widget.setWidget(content)
        return widget

    def update_preview(self, data: dict):
        """更新预览内容"""
        self.meta_labels['title'].setText(f"标题：{data.get('title', 'N/A')}")
        self.meta_labels['pages'].setText(f"页数：{data.get('pages', 0)}页")
        self.content_preview.setHtml(data.get('content', ''))